package zjc.bookkeeping.tools;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Selection;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import zjc.bookkeeping.R;

public class RemarksDialog extends Dialog implements View.OnClickListener {
    EditText remarksEt;
    Button cancelBtn,sureBtn;

    public void setRemarksEt(String str) {
        //获取已经输入的文字
        remarksEt.setText(str);
        //光标位于文字之后
        Selection.setSelection(remarksEt.getText(),remarksEt.getText().length());
    }

    public interface OnRemarksSureListener{
        public void onRemarksSure();
    }
    OnRemarksSureListener onRemarksSureListener;

    public void setOnRemarksSureListener(OnRemarksSureListener onRemarksSureListener) {
        this.onRemarksSureListener = onRemarksSureListener;
    }

    public RemarksDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_remarks);
        initView();
    }
//    加载控件
    private void initView() {
        remarksEt = findViewById(R.id.dialog_remarks_et);
        cancelBtn = findViewById(R.id.dialog_remarks_btn_cancel);
        sureBtn = findViewById(R.id.dialog_remarks_btn_sure);

        cancelBtn.setOnClickListener(this);
        sureBtn.setOnClickListener(this);
    }
//    点击事件
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.dialog_remarks_btn_cancel:
                cancel();
                break;
            case R.id.dialog_remarks_btn_sure:
                if (onRemarksSureListener != null){
                    onRemarksSureListener.onRemarksSure();
                }
                cancel();
                break;
        }
    }
//    获取备注信息
    public String getRemarksEt(){
        return remarksEt.getText().toString().trim();
    }
//    设置Dialog的控件尺寸
    public void setRemarksDialogSize(){
        Window window =getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        Display display = window.getWindowManager().getDefaultDisplay();
        layoutParams.width = (int) (display.getWidth());
        layoutParams.gravity = Gravity.BOTTOM;
        window.setAttributes(layoutParams);
        handler.sendEmptyMessageDelayed(1,100);
    }
//    设置键盘自动弹出
    Handler handler = new Handler(){
    @Override
    public void handleMessage(@NonNull Message msg){
        InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
    }
    };
}